package weekendhomework.homeword01;

import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class SubwaySystem {
    static Map<Integer, String>  sub;
    static {
        sub = new Subway().getSubway();
    }

    public void run(){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入您要上车的站点：");
        String mySub = scan.next();

        System.out.println("请输入您要下车的站点：");
        String userSub = scan.next(); // 用户要到达的位置

        if(haveThis(userSub) == -1 || haveThis(mySub) == -1){
            if(haveThis(userSub) == -1 ){
                System.out.println("上车站点错误（找不到该站点）");
            }else{
                System.out.println("下车站点错误错误（找不到该站点）");
            }
        }else{
            showTimeAndMoney(haveThis(mySub), haveThis(userSub));
        }
    }

    /**
     * 判断有没有该地铁站
     * @param s 被判断的地铁站
     * @return 若存在返回该地铁站的编号，不存在返回 -1
     */
    public Integer haveThis(String s){
        int key = -1;
        Set<Integer> keys = sub.keySet();
        for (Integer k : keys) {
            if(sub.get(k).equals(s)){
                key = k;
                break;
            }
        }
        return key;
    }

    public void showTimeAndMoney(int thisSub, int otherSub){
        int num = Math.abs(thisSub - otherSub); // 需要几个站

        int money = 0;
        int min = num * 2;

        if(num <= 3){
            money = 3;
        }else if(num > 3 && num<=5){
            money = 4;
        }else{
            money = (num - 5 ) * 2 + 4;

            if(money > 10){
                money = 10;
            }
        }

        System.out.printf("从%s到%s一共经过%d个站，收费%d元， 大约%d分钟",
                sub.get(thisSub), sub.get(otherSub),num, money, min);
    }
}
